import 'package:floor/floor.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/generated/locales.g.dart';

@Entity(tableName: 'heart_rate', indices: [
  Index(value: ['calendar', 'address'])
])
class HeartRateModel extends BaseModel {
  @ColumnInfo(name: 'calendar_time')
  late String? calendarTime;
  late String? calendar;
  late int? time;
  @ColumnInfo(name: 'start_time')
  late int? startTime;
  late int? rate;
  @ColumnInfo(name: 'resting_heart_rate')
  late int? restingHeartRate;
  @ColumnInfo(name: 'dynamic_heart_rate')
  late int? dynamicHeartRate;
  @ColumnInfo(name: 'heart_rate_max')
  late int? heartRateMax;
  @ColumnInfo(name: 'heart_rate_min')
  late int? heartRateMin;
  @ColumnInfo(name: 'heart_rate_avg')
  late int? heartRateAvg;

  HeartRateModel({
    super.id,
    super.address,
    super.dateCreated,
    super.dateModified,
    this.calendarTime,
    this.calendar,
    this.time,
    this.startTime,
    this.rate,
    this.restingHeartRate,
    this.dynamicHeartRate,
    this.heartRateMax,
    this.heartRateMin,
    this.heartRateAvg,
  });

  @override
  Map<String, dynamic> toMap() {
    return {
      'calendarTime': calendarTime,
      'calendar': calendar,
      'rate': rate,
      'heartRateMax': heartRateMax,
      'heartRateMin': heartRateMin,
      'heartRateAvg': heartRateAvg,
    };
  }

  ///
  get hrSummary => HRSummary(heartRate: heartRateAvg ??= 0);

  @override
  bool from(Map map) {
    // TODO: implement from
    throw UnimplementedError();
  }

  @override
  List<Object?> get props => [];
}

class HRSummary {
  late int heartRate;
  List<HRSummary> heartRates = [];
  late int heartRateMax = 0;
  late int heartRateMin = 0;
  late int heartRateAvg = 0;
  late String calendarTime = '';
  late String calendar = '';

  HRSummary.from(HeartRateModel model) {
    heartRateAvg = model.heartRateAvg ?? 0;
    heartRateMin = model.heartRateMin ?? 0;
    heartRateMax = model.heartRateMax ?? 0;
    calendar = model.calendar ?? '';
    calendarTime = model.calendarTime ?? '';
  }

  HRSummary({this.heartRate = 0, this.heartRateMin = 0, this.heartRateMax = 0, this.heartRateAvg = 0, this.calendar = '', this.calendarTime = ''});

  Map<String, dynamic> toMap() {
    return {
      'calendarTime': calendarTime,
      'calendar': calendar,
      'heartRateMax': heartRateMax,
      'heartRateMin': heartRateMin,
      'heartRateAvg': heartRateAvg,
    };
  }

  /// 获取当前心率, 在首页小组件显示
  get heartRateText => heartRate == 0 ? LocaleKeys.no_records_text.ts : '$heartRate bpm';

  /// 心率折线图上的tooltip
  get tooltipText {
    String ov = heartRateAvg == 0 ? '--' : '$heartRateAvg bpm';
    return '$ov \n $calendarTime';
  }
}